#coding: utf-8
import asyncore,socket
import codecs

class AsyncGet(asyncore.dispatcher):
    def __init__(self,host):
        asyncore.dispatcher.__init__(self)
        self.host=host
        self.create_socket(socket.AF_INET, \
                            socket.SOCK_STREAM)
        self.connect((host,80))
        self.request='GET /index.html HTTP/1.0\r\n\r\n'
        self.outf=None
        print 'Requesting index.html from',host

    def handle_connect(self):
        print 'Connect',self.host

    def handle_read(self):
        if not self.outf:
            print 'Creating',self.host
            self.outf=open('%s'%self.host,'wt')
        self.outf=open('%s'%self.host,'at')
        data=self.recv(8192)
        if data:
            self.outf.write(repr(data))

    def writeable(self):
        return len(self.request)>0

    def handle_write(self):
        #并非所有数据都被发送，所以跟踪检查一下
        num_sent=self.send(self.request)
        self.request=self.request[num_sent:]

    def handle_close(self):
        asyncore.dispatcher.close(self)
        print 'Socket closed for',self.host
        if self.outf:
            self.outf.close()

#获取一些数据

AsyncGet('www.yahoo.com')
<<<<<<< .mine
#AsyncGet('www.baidu.com')
=======
>>>>>>> .r46
asyncore.loop() #等待完成

